package com.shop.front.controller.store;

import com.shop.common.enums.store.StoreOrderStatusEnum;
import com.shop.front.vo.store.StoreOrderUserStatisticVo;
import com.shop.front.vo.store.StoreOrderVo;
import com.shop.common.partner.jiaotai.JiaotaiBaseRespVo;
import com.shop.front.request.store.StoreOrderSubmitReq;
import com.shop.front.service.store.StoreOrderService;
import com.shop.front.shiro.UserTokenUtil;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(description = "订单相关", position = -1)
@RestController
@RequestMapping(value = "/storeOrder", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class StoreOrderController {

    @Autowired
    private StoreOrderService storeOrderService;


    @ApiOperation(value = "提交订单")
    @ApiResponses(@ApiResponse(code = 10000, response = JiaotaiBaseRespVo.class, message = "content"))
    @RequestMapping("/orderSubmit")
    public BaseResponse orderSubmit(@RequestBody @Validated StoreOrderSubmitReq req) {
        Long userId = UserTokenUtil.getUser().getId();
        return storeOrderService.submit(userId, req);
    }

    @ApiOperation(value = "同步订单状态")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="orderNo", required = true, value = "订单号", paramType = "body"))
    @RequestMapping("/checkPayStatus")
    public BaseResponse checkPayStatus(@RequestBody @Ignore Map<String, String> map) {
        String orderNo = map.get("orderNo");
        if(StringUtils.isBlank(orderNo)) {
            return BaseResponse.fail("订单号不能为空！");
        }

        return storeOrderService.checkPayStatus(orderNo);
    }


    @ApiOperation(value = "去支付")
    @ApiResponses(@ApiResponse(code = 10000, response = Map.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="orderNo", required = true, value = "订单号", paramType = "body"))
    @RequestMapping("/goPay")
    public BaseResponse goPay(@RequestBody @Ignore Map<String, String> map) {
        String orderNo = map.get("orderNo");
        if(StringUtils.isBlank(orderNo)) {
            return BaseResponse.fail("订单号不能为空！");
        }

        return storeOrderService.goPay(orderNo);
    }

    @ApiOperation(value = "我的订单统计")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderUserStatisticVo.class, message = "content"))
    @RequestMapping("/myOrderStatistic")
    public BaseResponse myOrderStatistic(@RequestBody @Ignore Map<String, String> map) {
        Long userId = UserTokenUtil.getUser().getId();
        return storeOrderService.myOrderStatistic(userId);
    }

    @ApiOperation(value = "我的订单")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="status", required = true, value = "订单状态 -50 已取消 -40 已退款 0 待支付 10 待发货 20 待收货 30 待评价 40 已完成", paramType = "body"))
    @RequestMapping("/myOrder")
    public BaseResponse myOrder(@RequestBody @Validated Map<String, String> map) {
        Integer status = null;

        String statusParam = map.get("status");
        if(StringUtils.isNotBlank(statusParam)) {
            status = Integer.valueOf(statusParam);
        }

        Long userId = UserTokenUtil.getUser().getId();

        return storeOrderService.myOrder(userId, status);
    }

    @ApiOperation(value = "订单详情")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "订单id", paramType = "body"))
    @RequestMapping("/info")
    public BaseResponse info(@RequestBody @Validated Map<String, String> map) {
        Long id = WebContext.getSignId();
        return storeOrderService.info(id);
    }

    @ApiOperation(value = "订单号获取订单详情")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="orderNo", required = true, value = "订单号", paramType = "body"))
    @RequestMapping("/infoByOrderNo")
    public BaseResponse infoByOrderNo(@RequestBody @Validated Map<String, String> map) {
        String orderNo = map.get("orderNo");
        if(StringUtils.isBlank(orderNo)) {
            return BaseResponse.fail("订单号不能为空！");
        }
        return storeOrderService.infoByOrderNo(orderNo);
    }

    @ApiOperation(value = "取消订单")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "订单id", paramType = "body"))
    @RequestMapping("/cancel")
    public BaseResponse cancel(@RequestBody @Validated Map<String, String> map) {
        Long id = WebContext.getSignId();
        return storeOrderService.cancel(id);
    }

    @ApiOperation(value = "删除订单")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "订单id", paramType = "body"))
    @RequestMapping("/userDel")
    public BaseResponse userDel(@RequestBody @Validated Map<String, String> map) {
        Long id = WebContext.getSignId();
        return storeOrderService.userDel(id);
       // return BaseResponse.fail("暂不支持删除操作！");
    }


    @ApiOperation(value = "确认收货")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "订单id", paramType = "body"))
    @RequestMapping("/confirmReceipt")
    public BaseResponse confirmReceipt(@RequestBody @Validated Map<String, String> map) {
        Long id = WebContext.getSignId();
        return storeOrderService.confirmReceipt(id);
    }

    @ApiOperation(value = "订单商品购物车信息")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreOrderVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "订单商品购物车id", paramType = "body"))
    @RequestMapping("/orderCartInfo")
    public BaseResponse orderCartInfo(@RequestBody @Validated Map<String, String> map) {
        Long id = WebContext.getSignId();
        return storeOrderService.orderCartInfo(id);
    }


}
